package com.htwy.core.controller;

import com.htwy.core.model.Role;
import com.htwy.ext.BaseController;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;

public class RoleController extends BaseController<Role> {

    @Override
    protected Role dao() {
        return Role.dao;
    }

    public void removeRByUserIds(){
        String roleId = getPara("roleId");
        String[] userIds = getParaValues("userIds");

        if(StrKit.isBlank(roleId)){
            renderJson(Ret.fail().set("msg","角色不能为空"));
        }else{
            if(userIds != null && userIds.length > 0) {
                dao().removeRelationByUserIdsAndRoleId(userIds, roleId);
            }
            renderJson(Ret.ok());
        }
    }

    public void updateStatus(){
        String[] roleIds = getParaValues("roleIds");
        Integer status = getParaToInt("status");

        dao().updateStatus(roleIds, status);

        renderJson(Ret.ok());
    }

    public void getPermissionIds(){
        String roleId = getPara("roleId");
        if(StrKit.isBlank(roleId)){
            renderJson(Ret.fail().set("msg","角色不能为空"));
        }else{
            renderJson(Ret.ok().set("data", dao().getPermissionIdsById(roleId)));
        }
    }

    public void updateRolePermissions(){
        String roleId = getPara("roleId");
        String[] permissionIds = getParaValues("permissionIds");

        if(StrKit.isBlank(roleId)){
            renderJson(Ret.fail().set("msg","角色不能为空"));
        }else{
            dao().deletePermissionsByRoleId(roleId);
            if(permissionIds != null && permissionIds.length > 0){
                dao().addRolePermissions(roleId, permissionIds);
            }
            renderJson(Ret.ok());
        }
    }

}